// pages/activityCenter/refund.js
const app = getApp();
var topOpacity = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "申请退款", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent'
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,
    btnText: '提交',
    noteMaxLen: 300,
    partPro: '',
    openid: '',
    shareTitle: '',
    shareImg: '',
    id: 0,
    bmType: ''
  },
  tel() {
    wx.makePhoneCall({
      phoneNumber: '023-81395565',
    })
  },
  //退款理由
  partProVal(e) {
    this.setData({
      partPro: e.detail.value
    })
  },
  //退款理由
  partPro: function (e) {
    var value = e.detail.value,
      len = parseInt(value.length);
    if (len > this.data.noteMaxLen) return;
    this.setData({
      currentNoteLen: len //当前字数
    })
  },
  //提交
  tj() {
    var that = this;
    var partPro = that.data.partPro;
    if (partPro == "") {
      app.showTip("请填写退款理由");
      return;
    } else {
      wx.showLoading({
        title: '申请提交中...'
      });

      var requestPath = '';
      var bmType = that.data.bmType;
      if (bmType == 'activity') {
        //活动
        requestPath = 'addRefundInfo.php';
      } else if (bmType == 'space') {
        //空间预约
        requestPath = 'addSiteRefundInfo.php';
      } else if (bmType == 'mgy') {
        //蘑菇乐园
        requestPath = 'addMglyRefundInfo.php';
      } else if (bmType == 'newbm') {
        //原滩公园
        requestPath = 'addYtgyRefundInfo.php';
      }else if (bmType == 'xl') {
        //寻麓之旅
        requestPath = 'addXlRefundInfo.php';
      }
      wx.request({
        url: that.data.baseUrl + requestPath,
        method: 'POST',
        data: {
          'openid': that.data.openid,
          'id': that.data.id,
          'return_pay_content': partPro
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          wx.hideLoading();
          app.showTip(res.data.msg);
          if (res.data.status == 1) {
            //调用发放短信
            if (requestPath == 'addSiteRefundInfo.php' && res.data.isSend == 1) {
              //空间预约
              that.sendMsg(that.data.id);
            } else {
              //活动
              that.sendMsg(that.data.id);
            }

            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              });
            }, 1500);
          }
        },
        fail: function () {
          wx.hideLoading();
          app.showTip('抱歉,服务器繁忙,请稍候重试');
        }
      });
    }
  },

  //发送退款成功短信
  sendMsg(id) {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'sms/sendActivityRefundMsg.php',
      method: 'POST',
      data: {
        'id': id,
        'is_success': 1,
        'bmType': that.data.bmType
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {

      },
      fail: function () {

      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    var id = options.id;
    var bmType = options.bmType;
    if (id == 0 || id == null || id == '' || id == undefined) {
      app.showTip('抱歉,未获取到订单id哦');
      setTimeout(function () {
        wx.navigateBack({
          delta: 1
        });
      }, 1500);
      return;
    }
    that.setData({
      id: id,
      bmType: bmType
    });

    //获取缓存中的openid
    var openid = wx.getStorageSync('ly24_openid');
    if (openid == '') {
      //缓存中没有openid
      //获取所需页面信息
      wx.login({
        success: function (res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: that.data.baseUrl + 'getOpenid.php',
              method: 'POST',
              data: {
                'code': res.code
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function (res) {
                //缓存微信openid
                wx.setStorageSync('ly24_openid', res.data.openid);
                that.setData({
                  openid: res.data.openid
                });

                //获取本页数据
                that.getRefundPageInfo(res.data.openid, id, bmType);
              },
              fail: function (res) {}
            });
          }
        }
      });
    } else {
      that.setData({
        openid: openid
      });

      //获取本页数据
      that.getRefundPageInfo(openid, id, bmType);
    }

    //调用分享
    that.getShareInfo();
  },

  //获取本页数据
  getRefundPageInfo(openid, id, bmType) {
    var that = this;
    wx.showLoading({
      title: '数据加载中...'
    });
    var requestPath = '';
    if (bmType == 'activity') {
      //活动
      requestPath = 'getRefundPageInfo.php';
    } else if (bmType == 'space') {
      //空间预约
      requestPath = 'getSiteRefundPageInfo.php';
    } else if (bmType == 'mgy') {
      //蘑菇乐园
      requestPath = 'getMglyRefundPageInfo.php';
    } else if (bmType == 'newbm') {
      //原滩公园
      requestPath = 'getYtgyRefundPageInfo.php';
    }else if (bmType == 'xl') {
      //寻麓之旅
      requestPath = 'getXlRefundPageInfo.php';
    }
    wx.request({
      url: that.data.baseUrl + requestPath,
      method: 'POST',
      data: {
        'openid': openid,
        'id': id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.status == 1) {

        } else {
          app.showTip(res.data.msg);
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            });
          }, 2000);
        }
      },
      fail: function () {
        wx.hideLoading();
        app.showTip('抱歉,服务器繁忙');
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          });
        }, 2000);
      }
    });
  },


  //获取微信分享信息
  getShareInfo() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'getShareInfo.php',
      method: 'POST',
      data: {

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        that.setData({
          shareTitle: res.data.shareTitle,
          shareImg: res.data.shareImg
        });
      },
      fail: function (res) {}
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.shareTitle,
      path: '/pages/index/index',
      imageUrl: that.data.shareImg,
      success: function (res) {

      },
      fail: function (res) {

      }
    }
  }
})